/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.libraryItems.button {
	import flash.display.FrameLabel;
	import flash.events.Event;
	import flash.text.TextField;

	/**
	 * com.googlecode.milib.display.libraryItems.button.MCButtonIniMidEnd
	 * 
	 * The button clip should have frames:
	 *  - "ini" idle state and a beginig of rollover animation
	 *  - "mid" frame where animation stops if there's (stil) rollover or press state
	 *  - "end" frame where animation ends 
	 *  
	 * If clip contains any filters they will be used in press state
	 * 
	 * 
	 * @author Marek Brun
	 */
	public dynamic class MCButtonIniMidEnd extends AbstractMCButton {

		private var iniFilters:Array;		private var isRunning:Boolean;

		public function MCButtonIniMidEnd() {
			TextField.prototype;
			if(FrameLabel(currentLabels[0]).frame==1){
				gotoAndStop('ini');
			}
			if(filters.length){
				iniFilters=filters.concat();
				filters=[];
			}
		}
		
		override protected function doRollOver():void {
			addEventListener(Event.ENTER_FRAME, onEnterFrameWhenOver, false, 0, true);
			if(!isRunning){
				gotoAndPlay('ini');
				isRunning=true;
			}
		}
		
		override protected function doPress():void {
			if(iniFilters){
				filters=iniFilters;
			}
		}
		
		override protected function doRelease():void {
			if(iniFilters){
				filters=[];
			}
		}
		
		override protected function doRollOut():void {
			if(iniFilters){
				filters=[];
			}
			removeEventListener(Event.ENTER_FRAME, onEnterFrameWhenOver);
			addEventListener(Event.ENTER_FRAME, onEnterFrameWhenOut, false, 0, true);
			play();
			
		}
		
//********************************************************************************************
//		events for MCButtonIniMidEnd
//********************************************************************************************
		protected function onEnterFrameWhenOver(event:Event):void {
			if(currentLabel=='mid'){
				removeEventListener(Event.ENTER_FRAME, onEnterFrameWhenOver);
				gotoAndStop('mid');
			}
		}
		
		protected function onEnterFrameWhenOut(event:Event):void {
			if(currentLabel=='end'){
				removeEventListener(Event.ENTER_FRAME, onEnterFrameWhenOut);
				isRunning=false;
				if(isOver){
					doRollOver();
				}else{
					gotoAndStop('ini');
				}
			}
		}
		
	}
}
